package controllers;


import java.util.Map;

import dto.retjson.*;

import play.mvc.*;
import play.mvc.Http.Context;
import play.libs.*;

import services.user.AniLocalUserAuthService;
import utils.MVCUtils;
import views.html.*;

public class UserAuthController extends AuthController {
	
    // -- Authentication

    /**
     * Login page.
     */
    public static Result login() {
    	return ok(login.render("Login"));
        //return ok();
    }

    /**
     * Handle login form submission.
     */
    public static Result authenticate() {
    	String retNotice = null;
    	try {
    		Map<String, String> reqArgs = MVCUtils.getRequestArgs(Context.current().request());
			retNotice = AniLocalUserAuthService.authenticateUser(reqArgs);
			
		} catch (Exception e) {
			return ok(Json.toJson(new RetDataDto(false, e.getMessage())));
		}
    	
    	return ok(Json.toJson(new AuthDataDto(
    			true, 
    			retNotice,
	    		routes.Application.index().url()
    			)
	    	)
    	);
    }
    
    

    /**
     * Logout and clean the session.
     */
    public static Result logout() {
    	response().discardCookies("email");
        session().clear();
        flash("success", "You've been logged out");
        return redirect(
                routes.UserAuthController.login()
        );
    }

}